package com.kd.aop;

import java.util.Date;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.kd.bean.admin.Log_admin;
import com.kd.service.admin.ILog_adminService;

@Component
@Aspect
public class WebLogAspect {
	private Log_admin logAdmin = new Log_admin();
	
	@Autowired
	private ILog_adminService Logservice;
	
	//构建日志对象
	private Logger logger = Logger.getLogger(getClass());
	
	@Pointcut("execution(* com.kd.controller..*.*(..))")
	public void webLog() {
	
	}
	
	@Before("webLog()")
	public void doBefore(JoinPoint joinPoint) throws Throwable {
		
		// 接收到请求，记录请求内容
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		// 记录下请求内容
		//logger.info("---------------request----------------");
		//logger.info("URL : " + request.getRequestURL().toString());
		
		//logger.info("HTTP_METHOD : " + request.getMethod());
		//logger.info("IP : " + request.getRemoteAddr());
		Enumeration<String> enu = request.getParameterNames();
		//logger.info("===="+enu.hasMoreElements());
		
		
		logAdmin.setLog_request_url(request.getRequestURL().toString());
		logAdmin.setLog_request_method(request.getMethod());
		logAdmin.setLog_ip_address(request.getRemoteAddr());
		String paraCon = "";
		while (enu.hasMoreElements()) {
			String name = (String) enu.nextElement();
			//logger.info("name:" + name + ",value:" + request.getParameter(name));
			paraCon += "name:" + name + ",value:" + request.getParameter(name)+";";
		}
		if(paraCon=="") {paraCon="null";}
		logAdmin.setLog_request_para(paraCon);
	}
	@AfterReturning(returning = "ret", pointcut = "webLog()")
	public void doAfterReturning(Object ret) throws Throwable {
		logger.info("---------------response----------------");
		// 处理完请求，返回内容
		logger.info("RESPONSE : " + ret);
		String return_msg = "";
		if(ret!=null) {
			return_msg = ret.toString();
			
		}
		logAdmin.setLog_return_msg(return_msg);
		
	}
	
	@AfterThrowing(throwing = "e",pointcut = "webLog()")
	public void myAfterThrowalbe(Throwable e) {
		logAdmin.setLog_exception_msg(e.getMessage());
	}
	
	@After("webLog()")
	public void myAfter() {
		try {
			Logservice.insertLog(logAdmin);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			
		}
	}
}
